package com.etang.a2020b1006.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bizideal.smarthome.socket.ConstantUtil;
import com.bizideal.smarthome.socket.ControlUtils;
import com.etang.a2020b1019.R;
import com.etang.a2020b1019.tools.AppConfig;
import com.etang.a2020b1019.tools.DiyToast;

public class ValueActivity extends Fragment {

    private CheckBox cb_smo_state, cb_ill_state, cb_per_state, cb_temp_state, cb_pm_state;
    private EditText et_smo_values, et_ill_values_low, et_ill_values_max, et_pm_values_low, et_pm_values_max, et_temp_values_low, et_temp_values_max, et_per_values;
    private SharedPreferences sharedPreferences;
    private Button btn_values_save;
    private int min_time = 0;
    private CountDownTimer timer;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_value, null, false);
        initView(view);
        cb_ill_state.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                    sharedPreferences.edit().putString("ill_state", "开").commit();
                else
                    sharedPreferences.edit().putString("ill_state", "关").commit();
            }
        });
        cb_per_state.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                    sharedPreferences.edit().putString("per_state", "开").commit();
                else
                    sharedPreferences.edit().putString("per_state", "关").commit();
            }
        });
        cb_temp_state.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                    sharedPreferences.edit().putString("temp_state", "开").commit();
                else
                    sharedPreferences.edit().putString("temp_state", "关").commit();
            }
        });
        cb_pm_state.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                    sharedPreferences.edit().putString("pm_state", "开").commit();
                else
                    sharedPreferences.edit().putString("pm_state", "关").commit();
            }
        });
        cb_smo_state.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                    sharedPreferences.edit().putString("smo_state", "开").commit();
                else
                    sharedPreferences.edit().putString("smo_state", "关").commit();
            }
        });
        btn_values_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_ill_values_low.getText().toString().isEmpty()
                        || et_ill_values_max.getText().toString().isEmpty()
                        || et_per_values.getText().toString().isEmpty()
                        || et_pm_values_low.getText().toString().isEmpty()
                        || et_pm_values_max.getText().toString().isEmpty()
                        || et_smo_values.getText().toString().isEmpty()
                        || et_temp_values_low.getText().toString().isEmpty()
                        || et_temp_values_max.getText().toString().isEmpty()) {
                    DiyToast.showToast(getActivity(), "不能有空白项");
                } else {
                    sharedPreferences.edit().putString("ill_values_low", et_ill_values_low.getText().toString()).commit();
                    sharedPreferences.edit().putString("ill_values_max", et_ill_values_low.getText().toString()).commit();
                    sharedPreferences.edit().putString("pm_values_low", et_pm_values_low.getText().toString()).commit();
                    sharedPreferences.edit().putString("pm_values_max", et_pm_values_max.getText().toString()).commit();
                    sharedPreferences.edit().putString("temp_values_max", et_temp_values_max.getText().toString()).commit();
                    sharedPreferences.edit().putString("temp_values_low", et_temp_values_low.getText().toString()).commit();
                    sharedPreferences.edit().putString("per_values", et_per_values.getText().toString()).commit();
                    sharedPreferences.edit().putString("smo_values", et_smo_values.getText().toString()).commit();
                    min_time = Integer.valueOf(sharedPreferences.getString("per_values", null));
                    if (timer != null) {
                        timer.cancel();
                    }
                    timer.start();
                }
            }
        });
        cb_per_state.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sharedPreferences.edit().putString("per_state", "开").commit();
                } else {
                    sharedPreferences.edit().putString("per_state", "关").commit();
                }
            }
        });
        min_time = Integer.valueOf(sharedPreferences.getString("per_values", null));
        timer = new CountDownTimer(min_time * 1000 * 60, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                Log.e("TIME  ", "onTick（分钟）: " + String.valueOf(millisUntilFinished / 1000 / 60));
                Log.e("TIME  ", "onTick（秒）: " + String.valueOf(millisUntilFinished / 1000 % 60));
                if (AppConfig.per == 1) {
                    cb_ill_state.setChecked(true);
                    cb_smo_state.setChecked(true);
                    cb_temp_state.setChecked(true);
                    cb_pm_state.setChecked(true);
                    timer.cancel();
                    try {
                        Thread.sleep(500);
                    } catch (Exception e) {
                    }
                    timer.start();
                }
            }

            @Override
            public void onFinish() {
                cb_ill_state.setChecked(false);
                cb_smo_state.setChecked(false);
                cb_temp_state.setChecked(false);
                cb_pm_state.setChecked(false);
                timer.start();
            }
        };
        if (min_time != 0) {
            timer.start();
        }
        handler.post(time);
        return view;
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (sharedPreferences.getString("smo_state", null).equals("开")) {
                if (AppConfig.smo > Integer.valueOf(sharedPreferences.getString("smo_values", null))) {
                    ControlUtils.control(ConstantUtil.WarningLight, ConstantUtil.CHANNEL_ALL, ConstantUtil.OPEN);
                    ControlUtils.control(ConstantUtil.Fan, ConstantUtil.CHANNEL_ALL, ConstantUtil.OPEN);
                }
            }
            if (sharedPreferences.getString("ill_state", null).equals("开")) {
                if (AppConfig.ill > Integer.valueOf(sharedPreferences.getString("ill_values_max", null))) {
                    ControlUtils.control(ConstantUtil.Lamp, ConstantUtil.CHANNEL_ALL, ConstantUtil.CLOSE);
                } else if (AppConfig.ill < Integer.valueOf(sharedPreferences.getString("ill_values_low", null))) {
                    ControlUtils.control(ConstantUtil.Lamp, ConstantUtil.CHANNEL_ALL, ConstantUtil.OPEN);
                }
            }
            if (sharedPreferences.getString("pm_state", null).equals("开")) {
                if (AppConfig.pm < Integer.valueOf(sharedPreferences.getString("pm_values_low", null))) {
                    ControlUtils.control(ConstantUtil.Fan, ConstantUtil.CHANNEL_ALL, ConstantUtil.CLOSE);
                } else if (AppConfig.pm > Integer.valueOf(sharedPreferences.getString("pm_values_max", null))) {
                    ControlUtils.control(ConstantUtil.Fan, ConstantUtil.CHANNEL_ALL, ConstantUtil.OPEN);
                }
            }
            if (sharedPreferences.getString("temp_state", null).equals("开")) {
                if (AppConfig.temp > Integer.valueOf(sharedPreferences.getString("temp_values_max", null))) {
                    ControlUtils.control(ConstantUtil.RFID_Open_Door, ConstantUtil.CHANNEL_1, ConstantUtil.OPEN);
                } else if (AppConfig.temp < Integer.valueOf(sharedPreferences.getString("temp_values_low", null))) {
                    ControlUtils.control(ConstantUtil.RFID_Open_Door, ConstantUtil.CHANNEL_1, ConstantUtil.OPEN);
                }
            }
            handler.postDelayed(time, 1000);
        }
    };
    Runnable time = new Runnable() {
        @Override
        public void run() {
            Message msg = handler.obtainMessage();
            handler.sendMessage(msg);
        }
    };

    private void initView(View view) {
        sharedPreferences = getActivity().getSharedPreferences("rember", Context.MODE_MULTI_PROCESS);
        cb_ill_state = (CheckBox) view.findViewById(R.id.cb_ill_state);
        btn_values_save = (Button) view.findViewById(R.id.btn_values_save);
        cb_smo_state = (CheckBox) view.findViewById(R.id.cb_smo_state);
        cb_per_state = (CheckBox) view.findViewById(R.id.cb_per_state);
        cb_pm_state = (CheckBox) view.findViewById(R.id.cb_pm_state);
        cb_temp_state = (CheckBox) view.findViewById(R.id.cb_temp_state);
        et_ill_values_low = (EditText) view.findViewById(R.id.et_ill_values_low);
        et_ill_values_max = (EditText) view.findViewById(R.id.et_ill_values_max);
        et_temp_values_low = (EditText) view.findViewById(R.id.et_temp_values_low);
        et_temp_values_max = (EditText) view.findViewById(R.id.et_temp_values_max);
        et_pm_values_low = (EditText) view.findViewById(R.id.et_pm_values_low);
        et_pm_values_max = (EditText) view.findViewById(R.id.et_pm_values_max);
        et_per_values = (EditText) view.findViewById(R.id.et_per_values_low);
        et_smo_values = (EditText) view.findViewById(R.id.et_smo_values);
        et_temp_values_max.setText(sharedPreferences.getString("temp_values_max", null));
        et_temp_values_low.setText(sharedPreferences.getString("temp_values_low", null));
        et_ill_values_max.setText(sharedPreferences.getString("ill_values_max", null));
        et_ill_values_low.setText(sharedPreferences.getString("ill_values_low", null));
        et_pm_values_low.setText(sharedPreferences.getString("pm_values_max", null));
        et_pm_values_max.setText(sharedPreferences.getString("pm_values_max", null));
        et_smo_values.setText(sharedPreferences.getString("smo_values", null));
        et_per_values.setText(sharedPreferences.getString("per_values", null));
        if (sharedPreferences.getString("smo_state", null).toString().equals("开"))
            cb_smo_state.setChecked(true);
        if (sharedPreferences.getString("per_state", null).toString().equals("开"))
            cb_per_state.setChecked(true);
        if (sharedPreferences.getString("ill_state", null).toString().equals("开"))
            cb_ill_state.setChecked(true);
        if (sharedPreferences.getString("temp_state", null).toString().equals("开"))
            cb_temp_state.setChecked(true);
        if (sharedPreferences.getString("pm_state", null).toString().equals("开"))
            cb_pm_state.setChecked(true);
    }
}
